
const apiBaseUrl = `${process.env.UMI_APP_AI_CREATIVE_BOX_API_URL}/ai-creative-box/`;

export const api = async (url: string, data: any = null) => {
  try {
    let resp = {};
    if (data === null) {
      resp = await (await fetch(apiBaseUrl + url)).json();
    } else {
      resp = await (await fetch(apiBaseUrl + url, {
        method: 'POST',
        mode: 'cors',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
      })).json();
    }

    // @ts-ignore
    if (!resp.success) {
      console.error(`请求 ${url} 返回失败响应:`, resp);
      // @ts-ignore
      throw new Error(resp.msg);
    }

    // @ts-ignore
    return resp.data;
  } catch (e) {
    console.error(`请求 ${url} 失败:`, e);
    throw e
  }
}